/**
 * 
 */
package fhq.hcmute.edu.vn.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Rotation;

import fhq.hcmute.edu.vn.config.AppConfig;
import fhq.hcmute.edu.vn.config.PictureFrameSetting;

/**
 * @author nglehien Nov 14, 2012 - 2:50:41 PM
 */
public class PictureFrame extends JFrame {

	private static PictureFrame instance = null;
	private static final long serialVersionUID = 1L;
	private ImageIcon imageIcon;
	private BufferedImage image;
	private BufferedImage rootImage;
	private JLabel lblFrame;
	private Dimension dimension;

	public static PictureFrame getInstance() {
		if (instance == null) {
			instance = new PictureFrame();
		}
		return instance;
	}

	public void showFrame() {
		setVisible(true);
		setExtendedState(Frame.MAXIMIZED_BOTH);
	}

	public void setImage(BufferedImage image) {
		this.image = image;
		rootImage = copyImage(image);
		showPicture();
	}

	public void setImage(byte[] imgFromByte) throws IOException {
		this.image = ImageIO.read(new ByteArrayInputStream(imgFromByte));
		rootImage = copyImage(image);
		showPicture();
	}

	private void showPicture() {
		image = Scalr.resize(image, Scalr.Method.QUALITY,
				Scalr.Mode.FIT_TO_HEIGHT, dimension.width, dimension.height,
				Scalr.OP_ANTIALIAS);
		imageIcon = new ImageIcon(image);
		lblFrame.setIcon(imageIcon);
	}

	public void rotateLeft() {
		image = Scalr.rotate(image, Rotation.CW_270);
		rootImage = Scalr.rotate(rootImage, Rotation.CW_270);
		if (image.getWidth() > image.getHeight()) {
			image = Scalr.resize(rootImage, Scalr.Method.QUALITY,
					Scalr.Mode.FIT_TO_HEIGHT, dimension.width,
					dimension.height, Scalr.OP_ANTIALIAS);
		} else {
			image = Scalr.resize(image, Scalr.Method.QUALITY,
					Scalr.Mode.FIT_TO_HEIGHT, dimension.height,
					dimension.height, Scalr.OP_ANTIALIAS);
		}
		imageIcon = new ImageIcon(image);
		lblFrame.setIcon(imageIcon);
	}

	public void rotateRight() {

		Scalr.Method scaleMethod = null;
		int method = AppConfig.getInstance().getPictureFrameSetting()
				.getScaleMethod();
		if (method == PictureFrameSetting.AUTOMATIC) {
			scaleMethod = Scalr.Method.AUTOMATIC;
		} else if (method == PictureFrameSetting.BALANCED) {
			scaleMethod = Scalr.Method.BALANCED;
		} else if (method == PictureFrameSetting.QUALITY) {
			scaleMethod = Scalr.Method.QUALITY;
		} else if (method == PictureFrameSetting.SPEED) {
			scaleMethod = Scalr.Method.SPEED;
		} else if (method == PictureFrameSetting.ULTRA_QUALITY) {
			scaleMethod = Scalr.Method.ULTRA_QUALITY;
		}
		image = Scalr.rotate(image, Rotation.CW_90);
		rootImage = Scalr.rotate(rootImage, Rotation.CW_90);
		if (image.getWidth() > image.getHeight()) {
			image = Scalr.resize(rootImage, scaleMethod,
					Scalr.Mode.FIT_TO_HEIGHT, dimension.width,
					dimension.height, Scalr.OP_ANTIALIAS);
		} else {
			image = Scalr.resize(image, scaleMethod, Scalr.Mode.FIT_TO_HEIGHT,
					dimension.height, dimension.height, Scalr.OP_ANTIALIAS);
		}
		imageIcon = new ImageIcon(image);
		lblFrame.setIcon(imageIcon);
	}

	public PictureFrame() {

		setUndecorated(true);
		getContentPane().setBackground(Color.BLACK);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setType(Type.NORMAL);
		setAlwaysOnTop(true);
		setResizable(false);
		setBounds(100, 100, 450, 300);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		dimension = toolkit.getScreenSize();
		setSize(dimension);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout(0, 0));

		lblFrame = new JLabel("");
		lblFrame.setBackground(Color.BLACK);
		lblFrame.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblFrame);
	}

	public static BufferedImage copyImage(BufferedImage image) {
		return scaledImage(image, image.getWidth(), image.getHeight());
	}

	public static BufferedImage scaledImage(BufferedImage image, int width,
			int height) {
		BufferedImage newImage = createCompatibleImage(width, height);
		Graphics graphics = newImage.createGraphics();

		graphics.drawImage(image, 0, 0, width, height, null);

		graphics.dispose();
		return newImage;
	}

	public static BufferedImage createCompatibleImage(int width, int height) {
		GraphicsConfiguration configuration = GraphicsEnvironment
				.getLocalGraphicsEnvironment().getDefaultScreenDevice()
				.getDefaultConfiguration();
		return configuration.createCompatibleImage(width, height,
				Transparency.TRANSLUCENT);
	}
}
